﻿#include <iostream>
#include <vector>
using namespace std;


class Solution
{
public:
    // 面试题 17.14. 最小K个数
    vector<int> smallestK(vector<int>& arr, int k)
    {
        srand(time(nullptr));
        qsort(arr, 0, arr.size() - 1, k);
        return { arr.begin(), arr.begin() + k };
    }

    void qsort(vector<int>& arr, int l, int r, int k)
    {
        if (l >= r)
            return;

        int key = GetKey(arr, l, r);
        int left = l - 1, i = l, right = r + 1;
        while (i < right)
        {
            if (arr[i] < key)
                swap(arr[++left], arr[i++]);
            else if (arr[i] == key)
                i++;
            else
                swap(arr[--right], arr[i]);
        }

        int a = left - l + 1, b = right - left - 1;
        if (a > k)
            qsort(arr, l, left, k);
        else if (a + b >= k)
            return;
        else
            qsort(arr, right, r, k - a - b);

    }

    int GetKey(vector<int>& arr, int left, int right)
    {
        return arr[rand() % (right - left + 1) + left];
    }
};

int main()
{

    return 0;
}